package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.MallOrderAdapter;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.GoodsOrderTask;
import com.xgkj.diyiketang.task.MyOrderTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MallOrderActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
    private static final String TAG = MallOrderActivity.class.getSimpleName();
    private Context mContext;
    private PullToRefreshListView mListView;
    private MallOrderAdapter mAdapter;
    private int pageNo = 1;
    private int size = 10;
    private TextView mBack;
    private NoDataUtil mNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mallorder);
        mContext = this;
        initView();
    }

    private void initView(){
        mBack = (TextView) findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView = (PullToRefreshListView) findViewById(R.id.orderlist);
        mListView.init(mContext,true);
        mAdapter = new MallOrderAdapter(mContext);
        mListView.setAdapter(mAdapter,true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,contentLayout);
    }

    private void getMyOrderList(){
        if (pageNo ==0){
            mListView.setNoNextPage();
            return;
        }
        GoodsOrderTask task = new GoodsOrderTask(size,pageNo);
        task.addListener(myorderListener);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener myorderListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            if (pageNo ==1){
                mListView.onRefreshComplete();
            }else {
                mListView.onLoadDataComplete(false);
            }
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (pageNo ==1){
                mListView.onRefreshComplete();
                mListView.enableLoadMore();
            }else{
                mListView.onLoadDataComplete(true);
            }
            if (data != null){
                List<OrderInfo> list = new ArrayList<OrderInfo>();
                list = (List<OrderInfo>) data;
                if (pageNo ==1){
                    mAdapter.clearData();
                }
                mAdapter.updateData(list);
            }
            pageNo = result;

            if (mAdapter != null && mAdapter.getCount()== 0){
                mNoData.ListViewEmpty(mListView,null,null,-1);
            }else {
                mNoData.ListViewNoEmpty(mListView);
            }
        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getMyOrderList();
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        getMyOrderList();
    }
}
